//
// ModelInstance.cpp
//
/*
	A model instance is a container for a model. It handles any transforms needed before rendering 
	the model to screen.
*/

#include "Common.h"

// Data about the location of the model vertices is stored in local model coordinates. Render() transforms this to
// world coords before rendering.
void ModelInstance::Render()
{
    glPushMatrix();
    glMultMatrixf(_transform[0]);
    _model->Render();
    glPopMatrix();

    //for(auto childiterator = _children.begin(); childiterator != _children.end(); childiterator++)
    //{
    //    (*childiterator)->Render();
    //}
}

Vec3f ModelInstance::GetModelCenterInWorldCoords()
{
	// Apply all transforms first
	Vec3f boundingBoxMinWorldCoords = _transform.TransformPoint(_model->BoundingBoxMin());
	Vec3f boundingBoxMaxWorldCoords = _transform.TransformPoint(_model->BoundingBoxMax());

	//if(DEBUG) {
	//	printf("Bounding Box MIN (in World Coords): %f, %f, %f\n", boundingBoxMinWorldCoords.x, boundingBoxMinWorldCoords.y, boundingBoxMinWorldCoords.z);
	//	printf("Bounding Box MAX (in World Coords): %f, %f, %f\n", boundingBoxMaxWorldCoords.x, boundingBoxMaxWorldCoords.y, boundingBoxMaxWorldCoords.z);
	//}
	Vec3f summed = boundingBoxMinWorldCoords + boundingBoxMaxWorldCoords;
	Vec3f center = Vec3f(summed.x/2, summed.y/2, summed.z/2);

	return center;
}

Vec3f ModelInstance::GetBoundingBoxMin() {
	return _model->BoundingBoxMin();
}

Vec3f ModelInstance::GetBoundingBoxMax() {
	return _model->BoundingBoxMax();
}

//void ModelInstance::deleteModel() {
//	if (_model != NULL) {
//		//_model->deleteAssets();
//		delete _model;
//
//		// ++TODO: not sure if we actually need this:
//		_model = NULL;
//	}
//}

string ModelInstance::GetModelId() {
	return _modelId;
}